<template>
	<div class="login-container">
		<div class="login-content">
			<div class="login-left">
				<img src="@/assets/login-banner.svg" alt="登录页面展示" class="login-banner">
			</div>
			<div class="login-right">
				<div class="login-box">
					<h2 class="login-title">欢迎登录</h2>
					<div class="login-form">
						<el-input v-model="username" placeholder="请输入用户名" prefix-icon="el-icon-user"
							class="login-input"></el-input>
						<el-input v-model="password" placeholder="请输入密码" prefix-icon="el-icon-lock" type="password"
							class="login-input" @keyup.enter.native="handleLogin"></el-input>
						<el-input v-model="vcode" placeholder="请输入验证码" prefix-icon="el-icon-lock"
							class="login-input captcha-input" @blur="verifyCode">
							<template #suffix>
								<div class="captcha-suffix">
									<img :src="captchaUrl" alt="验证码" title="看不清,点击切换" class="captcha-image"
										@click="refreshCaptcha">
									<span class="refresh-icon" @click="refreshCaptcha">×</span>
								</div>
							</template>
						</el-input>
						<div class="remember-forgot">
							<el-checkbox v-model="rememberMe">记住我</el-checkbox>
							<a href="#" class="forgot-link">忘记密码？</a>
						</div>
						<el-button type="primary" class="login-btn" :disabled="!canLogin" @click="handleLogin">
							登录
						</el-button>
						<div class="register-link">
							还没有账号？<a href="#" class="link">立即注册</a>
						</div>
						<div class="agreement">
							<el-checkbox v-model="agreed">
								我已阅读并同意
								<a href="#" class="link">《非凡用户服务协议》</a>
								<a href="#" class="link">《隐私政策》</a>
							</el-checkbox>
						</div>
						<div class="customer-service">
							客服电话：<a href="tel:1010-5335" class="phone-link">1010-5335</a>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
	import {
		buildVCodeApi,
		customerLoginApi
	} from '@/api/user'

	export default {
		name: 'Login',
		data() {
			return {
				username: '',
				password: '',
				rememberMe: false,
				agreed: false,
				vcode: '',
				resultCode: '',
				codeOk: false,
				captchaUrl: ''
			}
		},
		computed: {
			canLogin() {
				return this.codeOk && this.username.length > 0 && this.password.length > 0 && this.agreed
			}
		},
		created() { //页面刷新时
			this.refreshCaptcha();
		},
		methods: {
			async refreshCaptcha() {
				const data = await buildVCodeApi();
				this.captchaUrl = data.results[1];
				this.resultCode = data.results[0];
				localStorage.setItem('vcodeId', data.results[2]);

			},
			verifyCode() {
				this.codeOk = false;
				if (this.vcode == this.resultCode) {
					this.codeOk = true;
				}
			},
			async handleLogin() {
				if (!this.canLogin) return;

				const data = await customerLoginApi(
					this.username,
					this.password,
					this.vcode,
					localStorage.getItem('vcodeId')
				);
				console.log(data.results);
				if (data.code == 200) {
					this.$store.state.username = data.results.username;
					// 登录成功后跳转到首页
					this.$router.push('/');
				} else {
					//登录失败
				}
			}
		}
	}
</script>

<style scoped>
	.login-container {
		min-height: 100vh;
		background-color: #f5f7fa;
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 40px 0;
	}

	.login-content {
		width: 1100px;
		height: 600px;
		display: flex;
		background: #fff;
		border-radius: 12px;
		overflow: hidden;
		box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
	}

	.login-left {
		flex: 1.2;
		background: linear-gradient(135deg, rgba(255, 107, 107, 0.1) 0%, rgba(78, 205, 196, 0.1) 100%);
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;
		overflow: hidden;
		padding: 40px;
	}

	.login-left::before {
		content: '';
		position: absolute;
		top: -50%;
		left: -50%;
		width: 200%;
		height: 200%;
		background: linear-gradient(45deg,
				transparent 0%,
				rgba(255, 107, 107, 0.1) 30%,
				rgba(78, 205, 196, 0.1) 70%,
				transparent 100%);
		animation: wave 8s linear infinite;
	}

	@keyframes wave {
		0% {
			transform: rotate(0deg);
		}

		100% {
			transform: rotate(360deg);
		}
	}

	.login-banner {
		width: 100%;
		height: 100%;
		position: relative;
		z-index: 1;
		filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.1));
		object-fit: contain;
		max-height: 520px;
	}

	.login-right {
		flex: 0.8;
		padding: 40px;
		display: flex;
		flex-direction: column;
		justify-content: center;
		background: #fff;
	}

	.login-title {
		font-size: 32px;
		color: #333;
		margin: 0 0 40px 0;
		text-align: center;
		font-weight: 600;
		background: linear-gradient(135deg, #FF6B6B 0%, #4ECDC4 100%);
		-webkit-background-clip: text;
		-webkit-text-fill-color: transparent;
		position: relative;
	}

	.login-title::after {
		content: '';
		position: absolute;
		bottom: -10px;
		left: 50%;
		transform: translateX(-50%);
		width: 40px;
		height: 3px;
		background: linear-gradient(135deg, #FF6B6B 0%, #4ECDC4 100%);
		border-radius: 3px;
	}

	.login-box {
		width: 100%;
	}

	.login-form {
		width: 100%;
	}

	.login-input {
		margin-bottom: 20px;
	}

	.login-input :deep(.el-input__inner) {
		height: 48px;
		line-height: 48px;
		border-radius: 24px;
		font-size: 16px;
		border: 2px solid #eee;
		transition: all 0.3s ease;
	}

	.login-input :deep(.el-input__inner):focus {
		border-color: #4ECDC4;
		box-shadow: 0 0 10px rgba(78, 205, 196, 0.2);
	}

	.login-input :deep(.el-input__prefix) {
		left: 15px;
	}

	.login-input :deep(.el-input__inner) {
		padding-left: 45px;
	}

	.remember-forgot {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 24px;
	}

	.forgot-link {
		color: #FF6B6B;
		text-decoration: none;
		font-size: 14px;
		transition: all 0.3s ease;
	}

	.forgot-link:hover {
		color: #4ECDC4;
	}

	.login-btn {
		width: 100%;
		height: 48px;
		font-size: 16px;
		border-radius: 24px;
		background: linear-gradient(135deg, #FF6B6B 0%, #4ECDC4 100%);
		border: none;
		margin-bottom: 20px;
		position: relative;
		overflow: hidden;
	}

	.login-btn::before {
		content: '';
		position: absolute;
		top: 0;
		left: -100%;
		width: 100%;
		height: 100%;
		background: linear-gradient(90deg,
				transparent 0%,
				rgba(255, 255, 255, 0.2) 50%,
				transparent 100%);
		transition: all 0.5s ease;
	}

	.login-btn:hover::before {
		left: 100%;
	}

	.login-btn:hover {
		transform: translateY(-2px);
		box-shadow: 0 4px 12px rgba(78, 205, 196, 0.3);
	}

	.register-link {
		text-align: center;
		margin-bottom: 24px;
		font-size: 14px;
		color: #666;
	}

	.link {
		color: #FF6B6B;
		text-decoration: none;
		transition: all 0.3s ease;
	}

	.link:hover {
		color: #4ECDC4;
	}

	.agreement {
		margin-bottom: 24px;
		font-size: 14px;
		color: #666;
	}

	.customer-service {
		text-align: center;
		font-size: 14px;
		color: #666;
	}

	.phone-link {
		color: #FF6B6B;
		text-decoration: none;
		transition: all 0.3s ease;
	}

	.phone-link:hover {
		color: #4ECDC4;
	}

	:deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
		background-color: #4ECDC4;
		border-color: #4ECDC4;
	}

	:deep(.el-checkbox__input.is-checked + .el-checkbox__label) {
		color: #4ECDC4;
	}

	:deep(.el-checkbox__inner:hover) {
		border-color: #4ECDC4;
	}

	/* 新增验证码样式 */
	.captcha-container {
		position: relative;
		margin-bottom: 20px;
	}

	.captcha-input :deep(.el-input__inner) {
		padding-right: 100px;
		/* 为验证码区域留出空间 */
	}

	.captcha-suffix {
		position: absolute;
		right: 15px;
		top: 50%;
		transform: translateY(-50%);
		display: flex;
		align-items: center;
		gap: 8px;
		cursor: pointer;
	}

	.captcha-image {
		width: 100px;
		height: 40px;
		border: 1px solid #eee;
		border-radius: 4px;
		object-fit: cover;
		cursor: pointer;
		transition: all 0.3s ease;
	}

	.captcha-image:hover {
		opacity: 0.9;
	}

	.refresh-icon {
		font-size: 18px;
		color: #999;
		line-height: 1;
	}

	.captcha-error {
		height: 20px;
		color: #ff4d4f;
		font-size: 14px;
		margin-top: 4px;
	}
</style>